/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package game;

import framework.GameObject;
import framework.Util;
import framework.gameAPI.Sprite;
import java.awt.Graphics;

/**
 *
 * @author aluno
 */
public class Block extends GameObject {
    private boolean visible;
    private Sprite sprite;
    private static final int[] ANIM_MOVE = {0,1,2,1};
    private static final int[] ANIM_DEAD = {3,4,5};
    private int state;
    private static final int STATE_MOVING_HORIZONTAL = 0;
    private static final int STATE_MOVING_VERTICAL   = 1;
    private static final int STATE_DYING  = 2;
    private static final int STATE_DEAD   = 3;
    private int contHorz;
    private int contVert;

    public Block(int x, int y) {
        this.x = x;
        this.y = y;
        image = Util.loadImage(Global.IMG_BLOCK);
        sprite = new Sprite(image, 50, 50);
        sprite.setAnimationDelay(5);
        sprite.setFrameSequence(ANIM_MOVE);
        this.visible = true;
        this.width = 50;
        this.height= 50;
        state = STATE_MOVING_HORIZONTAL;
        speedX = 1;
    }

    @Override
    public void update() {
        switch(state) {
            case STATE_MOVING_HORIZONTAL:
                if (contHorz++>=60) {
                    speedX = -speedX;
                    contHorz = 0;
                    if (contVert++>= 3) {
                        state = STATE_MOVING_VERTICAL;
                        contVert = 0;
                        speedY = 2;
                    }
                }
                x += speedX;
                break;
            case STATE_MOVING_VERTICAL:
                if (contVert++>=5) {
                    contVert = 0;
                    state = STATE_MOVING_HORIZONTAL;
                }
                y += speedY;
                break;
            case STATE_DYING:
                if (sprite.animationEnded()) {
                    state = STATE_DEAD;
                }
                break;
            case STATE_DEAD:
                setVisible(false);
                break;
        }
    }

    public void hit(){
        if (state != STATE_DYING) {
            state = STATE_DYING;
            sprite.setFrameSequence(ANIM_DEAD);
        }
    }

    @Override
    public void paint(Graphics g) {
        if (visible) {
            //g.drawImage(image, x, y, null);
            sprite.setX(x);
            sprite.setY(y);
            sprite.paint(g);
        }
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }
    
}
